package com.springboot.auth.authorization.oauth2;

import com.springboot.auth.authorization.entity.Role;
import com.springboot.auth.authorization.entity.User;
import com.springboot.auth.authorization.provider.OrganizationProvider;
import com.springboot.auth.authorization.service.IRoleService;
import com.springboot.auth.authorization.service.IUserService;
import com.springboot.auth.authorization.util.RedisUtils;
import com.springboot.cloud.common.core.utils.EncryptUtils;
import com.springboot.cloud.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by ZengJian on 2021/2/25 0025 下午 5:53
 */

@Slf4j
@Service("qrUserDetailsService")
public class QrUserDetailsService extends CustomUserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private IUserService userService;

//        @Autowired
//        private OrganizationProvider organizationProvider;

    @Override
    public UserDetails loadUserByUsername(String openId) throws UsernameNotFoundException {


//            User user = userService.getUserByOpenId("omd0X6yR16LjrjSjSxTz9NOmriBA");
        log.info("load user by openId:{}", openId);
        String sceneStr = userService.getSSceneStr(openId);
        if (StringUtils.isEmpty(sceneStr)) {
            //如果未取到则过期验证码已失效
            throw new UsernameNotFoundException("sceneStr已过期");
        }
        log.info("load user by sceneStr:{}", sceneStr);
        log.info("load user by openId:{}", openId);
        User user = userService.getUserByOpenId(openId);
//            User usera = userService.getUserByOpenId(sceneStr);
        if (user == null) {
            throw new UsernameNotFoundException("不存在该用户");
        }
        log.info("load user by mobile:{}", user.toString());

//            String credentials = organizationProvider.getSSceneStr(sceneStr);

        String credentials = passwordEncoder.encode(sceneStr);
        //首先比对验证码是否失效
//            String sceneStr= organizationProvider.getSSceneStr(openId); //传入tonkenId返回redis中的value

        System.out.println("credentials:" + credentials);
        return new org.springframework.security.core.userdetails.User(
                user.getOpenId(),
                credentials,
                user.getEnabled(),
                user.getAccountNonExpired(),
                user.getCredentialsNonExpired(),
                user.getAccountNonLocked(),
                super.obtainGrantedAuthorities(user));
    }
    /**
     * 获得登录者所有角色的权限集合.
     *
     * @param user
     * @return
     */
//    protected Set<GrantedAuthority> obtainGrantedAuthorities(User user) {
//        Set<Role> roles = roleService.queryUserRolesByUserId(user.getId());
//        log.info("user:{},roles:{}", user.getUsername(), roles);
//        return roles.stream().map(role -> new SimpleGrantedAuthority(role.getCode())).collect(Collectors.toSet());
//    }
}
